import Vue from 'vue';
import {loginPath} from './env';
import { Message,Loading} from 'element-ui';    
/* @desc:不拦截的请求名 */
const unInterceptorUrl = ['available']

//不弹loading的请求名
const unLoadingUrl = ['available','upload','wechat','activity-common','add','list']



/* @desc:检测请求名是否需要拦截 */
function checkUrl(resp){
    let arr = resp.config.url.split('/'),flag = true
    unInterceptorUrl.forEach(item=>{
        if(item === arr[arr.length-1]){
            flag = false
        }
    })
    return flag
}

/* @desc:检测不弹loading的请求名*/
function checkLoadUrl(resp){
    let arr = resp.url.split('/'),flag = true
    unLoadingUrl.forEach(item=>{
        if(item === arr[arr.length-1]){
            flag = false
        }
    })
    return flag
}

// let loadingInstance=null;

export const request = function(config){
    // loadingInstance=null;
    if(checkLoadUrl(config)){
        // loadingInstance =Loading.service({ fullscreen: true })
    }
    return config
}

export const response = function(response){
    // if(loadingInstance){
    //     loadingInstance.close(); 
    // }
    if(response.data.code==401){
        window.location.href=loginPath
    }
    if(response.data.code == 404){
        Message({
            showClose: true,
            message: '程序正在修复中'
        })
    }
    if((response.data.code!=200&&response.data.code!=0)&&checkUrl(response)){
        Message({
            showClose: true,
            message: response.data.message
        })
    }
    return response   
}



